package com.hua.api.modules.express.rest;

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;
import com.hua.api.core.framework.annotation.AnonymousAccess;
import com.hua.api.core.framework.util.Result;
import com.hua.api.modules.express.service.ShopInfoExpressService;
import com.hua.api.modules.express.service.impl.IssExpressInfoServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.Charset;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/iss/expressOrder/")
public class IssExpressOrderController {

    public final IssExpressInfoServiceImpl issExpressInfoService;

    /**
     * 方法描述：TODO 参考： http://open.ishansong.com/joinGuide/294
     * exp:
     {"clientId":"ss4suBdgGxz7CMY20","issOrderNo":"TDH2022031618102894","orderNo":"903834617463",
     "shopId":"20000000000001119","status":20,"statusDesc":"派单中",
     "subStatus":1,"subStatusDesc":"派单中","token":"CE8652B858BE9D9384FF33ED3E17A554"
     }
     * @auther luoyouhua
     * @version 1.0
     * @datetime 2022-03-16 17:40:15
     * Copyright © 2021 xiaoke66.cn All Rights Reserved
     */
    @AnonymousAccess
    @ResponseBody
    @RequestMapping(value = "receive",method = RequestMethod.POST)
    public JSONObject receive(HttpServletRequest request){
        log.info("IssExpressOrderController.receive()");
        // log.info("接收到闪送订单状态变更");
        JSONObject result = new JSONObject();
        try {
            //String body = IoUtil.read(request.getInputStream(),"utf-8");
            String body = IoUtil.read(request.getInputStream(),Charset.forName("utf-8"));
            log.info("IssExpressOrderController.receive()接收到闪送订单状态变更参数: [{}]",body);
            JSONObject data = JSONObject.parseObject(body);
            issExpressInfoService.onOrderStatusChange(data);
            result.put("status",200);
        } catch (IOException e) {
            result.put("status",300);

        }
        return result;
    }

}
